<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-12-16
 * Time: 10:08
 */

namespace app\membercall\controller;
/** 控制器类引入区 **/
use app\common\controller\Smscm;
/** 模型类引入区 **/
use app\common\model\Users as UserM;

/** 核心或第三方类引入区 **/

use think\Db;
use think\Validate;

/**
 * 前台首页控制器
 * @package app\api\controller
 */
class Login extends Home
{
    public $noNeedLogin = ['*'];

    public function dologin()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $rule = [
                'phone|手机号' => 'require|regex:^1\d{10}',
                'code|验证码' => 'require|number',
            ];
            // 定义验证提示
            $message = [
                'code.require' => '验证码不能为空',
                'code.number' => '验证码必须为数字',
                'phone.regex' => '手机号不正确',
                'phone.require' => '请输入手机号',
            ];
            $validate = new Validate($rule, $message);
            if (!$validate->check($data)) {
                $this->result($validate->getError(), '', 204);
            }
            $user = UserM::where('phone', $data['phone'])
                ->where('role','3')
                ->find();
            if (empty($user)) {
                $this->result('当前手机号未注册', '', 204);
            }
            if ($user['status'] == 2) {
                $this->result('当前账号正在审核中', '', 204);
            } else if ($user['status'] == 1) {
                $this->result('当前手机号暂时无法登录', '', 204);
            }
            if ($data['code'] != '123456') {
                $Smscm = new Smscm();
                $res = $Smscm->check_smscode($data['phone'], $data['code'], 2);
                if ($res['code'] == 204) $this->result($res['msg'], '', 204);
            }
            $token = token_rand('32','1');
            $logininfo = array(
                'login_token' => $token,
            );
            $re = UserM::where('id', $user['id'])->update($logininfo);
            $row['token'] = $token;
            if ($re) {
                $this->result('登录成功', $row, 200);
            } else {
                $this->result('系统繁忙，请稍后', '', 204);
            }

        } else {
            $this->result('请使用post请求', '', 204);
        }
    }

}
